// pages/appointment/appointment.js
var app = getApp();
// pages/staffLogin/staffLogin.js
Page({
  data: {
    username: '', // 账号
    password: '', // 密码
    loading: false // 登录加载状态
  },
onLoad() {
	  // 页面加载时的逻辑
	  let self = this;
	  let aid = '';
	    if(!aid){
	  	  aid=wx.getStorageSync('VP_AID');
	    }	    
	    app.util.getUserInfo(function (response) {
	    	console.log("==获取当前用户信息1==");
	    	console.log(response);
	    	self.setData({
	    		userInfo: response
	    	});
	    	console.log("==应用初始化：应用配置/应用中用户信息/当前城市信息==");
	    	
	    	setTimeout(function(){
	    		app.mdInit(function (cfg,mine,area) {
	    			self.setData({
	    				cfg: cfg,
	    				mine:mine,
	    				area:area
	    			});
	    		},{
	    				aid:self.data.aid,
	    				fuid:self.data.fuid,
	    				stid:self.data.fuid
	    		},true);
	    	},500)
	    });    
  },
  // 账号输入变化
  onUsernameChange(e) {
    this.setData({
      username: e.detail.value.trim()
    });
  },

  // 密码输入变化
  onPasswordChange(e) {
    this.setData({
      password: e.detail.value
    });
  },

  // 处理登录
  handleLogin() {
    const { username, password } = this.data;
    
    // 简单前端验证
    if (!username) {
      wx.showToast({
        title: '请输入账号',
        icon: 'none'
      });
      return;
    }

    if (!password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      });
      return;
    }

    this.setData({ loading: true });
	app.util.request({
		url: 'Hospital/login',
		data: {
			username,
			password
		  },
		method:'POST',
		success(res) {
			if (res.data.code === 1) {
			  // 登录成功
			  wx.showToast({
			    title: '登录成功',
			    icon: 'success'
			  });
			  // 跳转到工作台
			  setTimeout(() => {
			    wx.redirectTo({
			      url: `/vp_pz/pages/admin/center?hospital_id=`+res.data.data.id
			    })
			  }, 1500);
			} else {
			  // 登录失败
			  wx.showToast({
			    title: res.data.message || '登录失败',
			    icon: 'none'
			  });
			}
		},
      fail: (err) => {
		  console.log(err)
        wx.showToast({
          title: err.data.msg,
          icon: 'none'
        });
      },
      complete: () => {
        this.setData({ loading: false });
      }
	})
  },

});